/*
 * Copyright 2013 Oliver Schinagl
 *
 * Oliver Schinagl <oliver@schinagl.nl>
 *
 * This file is dual-licensed: you can use it either under the terms
 * of the GPL or the X11 license, at your option. Note that this dual
 * licensing only applies to this file, and not this project as a
 * whole.
 *
 *  a) This file is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of the
 *     License, or (at your option) any later version.
 *
 *     This file is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 * Or, alternatively,
 *
 *  b) Permission is hereby granted, free of charge, to any person
 *     obtaining a copy of this software and associated documentation
 *     files (the "Software"), to deal in the Software without
 *     restriction, including without limitation the rights to use,
 *     copy, modify, merge, publish, distribute, sublicense, and/or
 *     sell copies of the Software, and to permit persons to whom the
 *     Software is furnished to do so, subject to the following
 *     conditions:
 *
 *     The above copyright notice and this permission notice shall be
 *     included in all copies or substantial portions of the Software.
 *
 *     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *     OTHER DEALINGS IN THE SOFTWARE.
 */

/dts-v1/;
#include "sun7i-a20.dtsi"
#include "sunxi-common-regulators.dtsi"

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/interrupt-controller/irq.h>
#include <dt-bindings/pinctrl/sun4i-a10.h>

/ {
	model = "Cubietech Cubietruck";
	compatible = "cubietech,cubietruck", "allwinner,sun7i-a20";

	aliases {
		serial0 = &uart0;
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	leds {
		compatible = "gpio-leds";
		pinctrl-names = "default";
		pinctrl-0 = <&led_pins_cubietruck>;

		blue {
			label = "cubietruck:blue:usr";
			gpios = <&pio 7 21 GPIO_ACTIVE_HIGH>;
		};

		orange {
			label = "cubietruck:orange:usr";
			gpios = <&pio 7 20 GPIO_ACTIVE_HIGH>;
		};

		white {
			label = "cubietruck:white:usr";
			gpios = <&pio 7 11 GPIO_ACTIVE_HIGH>;
		};

		green {
			label = "cubietruck:green:usr";
			gpios = <&pio 7 7 GPIO_ACTIVE_HIGH>;
		};
	};

	reg_vmmc3: vmmc3 {
		compatible = "regulator-fixed";
		pinctrl-names = "default";
		pinctrl-0 = <&vmmc3_pin_cubietruck>;
		regulator-name = "vmmc3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		enable-active-high;
		gpio = <&pio 7 9 GPIO_ACTIVE_HIGH>;
	};
};

&ahci {
	target-supply = <&reg_ahci_5v>;
	status = "okay";
};

&cpu0 {
	cpu-supply = <&reg_dcdc2>;
};

&ehci0 {
	status = "okay";
};

&ehci1 {
	status = "okay";
};

&gmac {
	pinctrl-names = "default";
	pinctrl-0 = <&gmac_pins_rgmii_a>;
	phy = <&phy1>;
	phy-mode = "rgmii";
	status = "okay";

	phy1: ethernet-phy@1 {
		reg = <1>;
	};
};

&i2c0 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c0_pins_a>;
	status = "okay";

	axp209: pmic@34 {
		reg = <0x34>;
		interrupt-parent = <&nmi_intc>;
		interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
	};
};

&i2c1 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c1_pins_a>;
	status = "okay";
};

&i2c2 {
	pinctrl-names = "default";
	pinctrl-0 = <&i2c2_pins_a>;
	status = "okay";
};

&ir0 {
	pinctrl-names = "default";
	pinctrl-0 = <&ir0_rx_pins_a>;
	status = "okay";
};

&mmc0 {
	pinctrl-names = "default";
	pinctrl-0 = <&mmc0_pins_a>, <&mmc0_cd_pin_reference_design>;
	vmmc-supply = <&reg_vcc3v3>;
	bus-width = <4>;
	cd-gpios = <&pio 7 1 GPIO_ACTIVE_HIGH>; /* PH1 */
	cd-inverted;
	status = "okay";
};

&mmc3 {
	pinctrl-names = "default";
	pinctrl-0 = <&mmc3_pins_a>;
	vmmc-supply = <&reg_vmmc3>;
	bus-width = <4>;
	non-removable;
	status = "okay";

	brcmf: bcrmf@1 {
		reg = <1>;
		compatible = "brcm,bcm4329-fmac";
		interrupt-parent = <&pio>;
		interrupts = <7 10 IRQ_TYPE_LEVEL_LOW>; /* PH10 / EINT10 */
		interrupt-names = "host-wake";
	};
};

&mmc3_pins_a {
	/* AP6210 requires pull-up */
	allwinner,pull = <SUN4I_PINCTRL_PULL_UP>;
};

&nfc {
	pinctrl-names = "default";
	pinctrl-0 = <&nand_pins_a>, <&nand_cs0_pins_a>, <&nand_rb0_pins_a>;
	status = "okay";

	nand@0 {
		#address-cells = <2>;
		#size-cells = <2>;
		reg = <0>;
		allwinner,rb = <0>;
		nand-ecc-mode = "hw";
		nand-rnd-mode = "hw";

		boot0@0 {
			label = "boot0";
			reg = /bits/ 64 <0x0 0x200000>;
			nand-ecc-mode = "hw_syndrome";
			nand-rnd-mode = "hw";
			nand-randomizer-seeds = /bits/ 16 <0x4a80>;
		};

		boot0-rescue@200000 {
			label = "boot0-rescue";
			reg = /bits/ 64 <0x200000 0x200000>;
			nand-ecc-mode = "hw_syndrome";
			nand-rnd-mode = "hw";
			nand-randomizer-seeds = /bits/ 16 <0x4a80>;
		};

		main@200000 {
			label = "main";
			reg = /bits/ 64 <0x400000 0x1ffc00000>;
			nand-ecc-mode = "hw";
			nand-rnd-mode = "hw";
			nand-randomizer-seeds = /bits/ 16 <
				0x2b75 0x0bd0 0x5ca3 0x62d1 0x1c93 0x07e9 0x2162 0x3a72
				0x0d67 0x67f9 0x1be7 0x077d 0x032f 0x0dac 0x2716 0x2436
				0x7922 0x1510 0x3860 0x5287 0x480f 0x4252 0x1789 0x5a2d
				0x2a49 0x5e10 0x437f 0x4b4e 0x2f45 0x216e 0x5cb7 0x7130
				0x2a3f 0x60e4 0x4dc9 0x0ef0 0x0f52 0x1bb9 0x6211 0x7a56
				0x226d 0x4ea7 0x6f36 0x3692 0x38bf 0x0c62 0x05eb 0x4c55
				0x60f4 0x728c 0x3b6f 0x2037 0x7f69 0x0936 0x651a 0x4ceb
				0x6218 0x79f3 0x383f 0x18d9 0x4f05 0x5c82 0x2912 0x6f17
				0x6856 0x5938 0x1007 0x61ab 0x3e7f 0x57c2 0x542f 0x4f62
				0x7454 0x2eac 0x7739 0x42d4 0x2f90 0x435a 0x2e52 0x2064
				0x637c 0x66ad 0x2c90 0x0bad 0x759c 0x0029 0x0986 0x7126
				0x1ca7 0x1605 0x386a 0x27f5 0x1380 0x6d75 0x24c3 0x0f8e
				0x2b7a 0x1418 0x1fd1 0x7dc1 0x2d8e 0x43af 0x2267 0x7da3
				0x4e3d 0x1338 0x50db 0x454d 0x764d 0x40a3 0x42e6 0x262b
				0x2d2e 0x1aea 0x2e17 0x173d 0x3a6e 0x71bf 0x25f9 0x0a5d
				0x7c57 0x0fbe 0x46ce 0x4939 0x6b17 0x37bb 0x3e91 0x76db>;
		};
	};
};

&ohci0 {
	status = "okay";
};

&ohci1 {
	status = "okay";
};

&pio {
	vmmc3_pin_cubietruck: vmmc3_pin@0 {
		allwinner,pins = "PH9";
		allwinner,function = "gpio_out";
		allwinner,drive = <SUN4I_PINCTRL_10_MA>;
		allwinner,pull = <SUN4I_PINCTRL_NO_PULL>;
	};

	ahci_pwr_pin_cubietruck: ahci_pwr_pin@1 {
		allwinner,pins = "PH12";
		allwinner,function = "gpio_out";
		allwinner,drive = <SUN4I_PINCTRL_10_MA>;
		allwinner,pull = <SUN4I_PINCTRL_NO_PULL>;
	};

	led_pins_cubietruck: led_pins@0 {
		allwinner,pins = "PH7", "PH11", "PH20", "PH21";
		allwinner,function = "gpio_out";
		allwinner,drive = <SUN4I_PINCTRL_10_MA>;
		allwinner,pull = <SUN4I_PINCTRL_NO_PULL>;
	};

	usb0_vbus_pin_a: usb0_vbus_pin@0 {
		allwinner,pins = "PH17";
		allwinner,function = "gpio_out";
		allwinner,drive = <SUN4I_PINCTRL_10_MA>;
		allwinner,pull = <SUN4I_PINCTRL_NO_PULL>;
	};
};

&pwm {
	pinctrl-names = "default";
	pinctrl-0 = <&pwm0_pins_a>, <&pwm1_pins_a>;
	status = "okay";
};

&reg_ahci_5v {
	pinctrl-0 = <&ahci_pwr_pin_cubietruck>;
	gpio = <&pio 7 12 GPIO_ACTIVE_HIGH>;
	status = "okay";
};

#include "axp209.dtsi"

&reg_dcdc2 {
	regulator-always-on;
	regulator-min-microvolt = <1000000>;
	regulator-max-microvolt = <1450000>;
	regulator-name = "vdd-cpu";
};

&reg_dcdc3 {
	regulator-always-on;
	regulator-min-microvolt = <1000000>;
	regulator-max-microvolt = <1400000>;
	regulator-name = "vdd-int-dll";
};

&reg_ldo1 {
	regulator-name = "vdd-rtc";
};

&reg_ldo2 {
	regulator-always-on;
	regulator-min-microvolt = <3000000>;
	regulator-max-microvolt = <3000000>;
	regulator-name = "avcc";
};

&reg_usb0_vbus {
	pinctrl-0 = <&usb0_vbus_pin_a>;
	gpio = <&pio 7 17 GPIO_ACTIVE_HIGH>;
	status = "okay";
};

&reg_usb1_vbus {
	status = "okay";
};

&reg_usb2_vbus {
	status = "okay";
};

&uart0 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart0_pins_a>;
	status = "okay";
};

&usbphy {
	usb0_vbus-supply = <&reg_usb0_vbus>;
	usb1_vbus-supply = <&reg_usb1_vbus>;
	usb2_vbus-supply = <&reg_usb2_vbus>;
	status = "okay";
};
